package com.damagez.selfrefresh;

import com.damagez.selfrefresh.redis.RefreshCacheCollectAspect;
import com.damagez.selfrefresh.redis.RefreshRedisCacheKeyGenerator;
import com.damagez.selfrefresh.redis.RefreshRedisCacheManager;
import com.damagez.selfrefresh.redis.RefreshRedisCacheWriter;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * @author damage
 * @create 2020/5/12 9:42
 * @description
 */
@EnableCaching
@Configuration
public class RefreshRedisCacheAutoConfiguration {

    private final RedisConnectionFactory redisConnectionFactory;

    public RefreshRedisCacheAutoConfiguration(RedisConnectionFactory redisConnectionFactory) {
        this.redisConnectionFactory = redisConnectionFactory;
    }

    @Bean(RefreshRedisCacheWriter.BEAN_NAME)
    public RefreshRedisCacheWriter refreshRedisCacheWriter(){
        return new RefreshRedisCacheWriter(redisConnectionFactory);
    }

    @Bean(RefreshRedisCacheManager.BEAN_NAME)
    public CacheManager refreshRedisCacheManager(){
        return new RefreshRedisCacheManager(refreshRedisCacheWriter());
    }

    @Bean(RefreshRedisCacheKeyGenerator.BEAN_NAME)
    public KeyGenerator refreshRedisCacheKeyGenerator(){
        return new RefreshRedisCacheKeyGenerator();
    }

    @Bean
    public RefreshCacheCollectAspect refreshCacheCollectAspect(){
        return new RefreshCacheCollectAspect(refreshRedisCacheKeyGenerator());
    }
}
